/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.is.city;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.swing.JMapPane;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.event.MapMouseListener;

/**
 *
 * @author oleg
 */
public class MyMouseAdapter implements MapMouseListener{
    JMapPane mapPane;
    double clickToZoom = 0.1;

    Point point;
    Coordinate centre;
    Point mouselocation;

    private Point panePos;
    boolean panning;

    DirectPosition2D mapPosition;
    Envelope env;
    
    public MyMouseAdapter(JMapPane mapPane) {
        this.mapPane = mapPane;
    }
    
    @Override
    public void onMouseClicked(MapMouseEvent ev) {
    }

    @Override
    public void onMouseDragged(MapMouseEvent ev) {
        if (panning) {
            Point pos = ev.getPoint();
            if (!pos.equals(panePos)) {
                mapPane.moveImage(pos.x - panePos.x, pos.y - panePos.y);
                panePos = pos;
            }
        }
    }

    @Override
    public void onMouseEntered(MapMouseEvent ev) {
    }

    @Override
    public void onMouseExited(MapMouseEvent ev) {
    }

    @Override
    public void onMouseMoved(MapMouseEvent ev) {
    }

    @Override
    public void onMousePressed(MapMouseEvent ev) {
        ReferencedEnvelope displayArea = mapPane.getDisplayArea();
        mapPosition = ev.getMapPosition();
        point = ev.getPoint();
        
        panePos = ev.getPoint();
        panning = true;
    }

    @Override
    public void onMouseReleased(MapMouseEvent ev) {
        panning = false;
        mapPane.repaint();
    }

    @Override
    public void onMouseWheelMoved(MapMouseEvent ev) {
        int clicks = ev.getWheelAmount();
           // -ve means wheel moved up, +ve means down
           int sign = (clicks < 0 ? -1 : 1);

           Envelope env = mapPane.getDisplayArea();
           double width = env.getWidth();
           double delta = width * clickToZoom * sign;

           env.expandBy(delta);
           mapPane.setDisplayArea((org.opengis.geometry.Envelope) env);
           mapPane.repaint();
    }

}
